package com.ldzy.exception;

import com.ldzy.controller.MyResponse;
import com.ldzy.controller.ResultCode;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(ArithmeticException.class)
    public MyResponse handlerArithmeticException(ArithmeticException e){
        //在异常处理方法中一般要干三件事:1.根据异常紧急程度,发送邮件或短信给维护人员,2.打日志到日志文件中,3.将异常信息返回给前端
        return new MyResponse(ResultCode.ERROR,e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public MyResponse handlerMethodArgumentNotValidException(MethodArgumentNotValidException e){
        return new MyResponse(ResultCode.ERROR_PARAM,e.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(LoginException.class)
    public MyResponse handlerLoginException(LoginException e){
        return new MyResponse(e.getResultCode());
    }

}
